var $dataChart;

require(['grid', 'topBar', 'layout','date'], function () {
    //此参数用于查询，导出文档
    var postData;

    $('#top').topBar({       
        left: [{
                   type: 'filter-more',
                   dateType:'custom',
                   search: function () {
							if(getPostData()){
                                $('#grid').grid('setGridParam', {
                                    page:1,
                                    datatype : "json",
                                    postData : postData
                                }).trigger("reloadGrid");
							}
					},
                   item: [
                       {
                           text: '时间快捷',
                           name1: 'beginTime',
                           name2: 'endTime',
                           type: 'dateLine'
                       }
                   ]
               }],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                        	exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                        	exportFile('Pdf');
                        }
                    }
                ]
            },
            {
                text: '电压电流图表',
                type: 'btn',
                authority: '',
                click: function () {
                	dataChart('电压电流图表');
                }
            },
            {
                text: '车速转矩图表',
                type: 'btn',
                authority: '',
                click: function () {
                	dataChart('车速转矩图表');
                }
            },
            {
                text: '车速转速图表',
                type: 'btn',
                authority: '',
                click: function () {
                	dataChart('车速转速图表');
                }
            }
        ]
    });

    $('body').layout();

    //获取左侧选择的节点信息
    function getPostData(){
        postData=$("#top").parent().getValues();
        var type = commonFun.getLeftTreeCurrentTreeType();
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if(!orgIds||orgIds.length<1){
            $.messager.alert("提示","请选择查询车辆/机构/驾驶员。","info");
            return false;
        }
        postData.type = type;
        postData.orgIds =orgIds;
        return true;
    }
    
    function exportFile(suffix){
		if(getPostData()){
            var url = "/eltMhy/exportEltMhy"+suffix+".htm";
            $.exportFile(url,postData);
		}
    }

    $('#grid').grid({
    	url : "/eltMhy/searchEltMhyManagePage.htm",
        multiselect:false,
		colModel:[
		    {name: "rcrdId", label: "id", hidden:true},
			{name: "licPltNo", label: "车牌号", width:"70"},
			{name: "lineName", label: "营运线路", width:"70"},
			{name: "organName", label: "所属机构", width:"70"},
			{name: "eltMhyNo", label: "型号", width:"70"},
			{name: "rcrdTime",  label: "采集时间", width:"130"},
			{name: "totalMile", label: "总里程", align: "right", width:"70"},
			{name: "spdOfMotVhi", label: "车速", align: "right", width:"70"},
			{name: "eltMhyActualRtteSpd", label: "转速", align: "right", width:"70"},
			{name: "eltMhyActualTorque", label: "转矩", align: "right", width:"70"},
			{name: "gears", label: "档位", align: "right", width:"70"},
			{name: "eltMhyCtrlBusbarVtg", label: "DMC母线电压", align: "right", width:"110"},
			{name: "eltMhyCtrlBusbarEltCrt", label: "DMC母线电流",align: "right", width:"110"},
			{name: "eltMhyCtrlTept", label: "控制器温度",align: "right", width:"110"},
			{name: "eltMhyTept", label: "电机温度", align: "right", width:"110"},
			{name: "dmcVrsnNo", label: "DMC版本号", width:"110"},
			{name: "ivteTept", label: "逆变器温度", align: "right", width:"110"},
			{name: "hybridReadySt", label: "Hybrid Ready状态", width:"130",dict:"HYBR0000"},
			{name: "eltMhyCtrlPreEndVtg", label: "电机前端电压", align: "right", width:"110"},
			{name: "eltMhyCtrlLastEndVtg", label: "电机后端电压", align: "right", width:"110"},
			{name: "eltMhyDcVtg", label: "电机直流电压", align: "right", width:"110"},
			{name: "eltMhyEltCrt", label: "电机相电流", align: "right", width:"110"},
		    {name: "eltMhyVtg", label: "电机相电压", align: "right", width:"110"}]
    });

    function dataChart(title){
        var nodes = commonFun.getLeftTreeCheckedNodes("CAR",true);
        if(!nodes){
            $.messager.alert("提示", "请选择查询车辆。", "info");
            return;
        }
        if(nodes.length>1) {
            $.messager.alert("提示","只能选择一辆车。","info");
            return;
        }
        var chartParams = $('#top').parent().getValues();
        var beginTime = chartParams.beginTime;
        var endTime = chartParams.endTime;
        if(beginTime.substr(0,10) != endTime.substr(0,10)){
            $.messager.alert("提示", "只能分析一天内的数据,请重新选择日期。", "info");
            return;
        }
        chartParams.orgIds=nodes[0].id;
        chartParams.carNo = nodes[0].cd;
        chartParams.funcId = "01";
        chartParams.type="CAR";
        if(!$dataChart){
            $dataChart=$('<div>').dialog({
                    url: './mainte/report/viewDataChart.htm',
                    height: 550,
                    width: 720,
                    buttons:[
                     {
                         text: '关闭',
                         css: 'btn-cancel',
                         click: function () {
                             $(this).dialog('close');
                         }
                     }
                 ]
                });
        }
        if(title=='车速转速图表'){
            $dataChart.dialog('exec', 'spdAndEMSpdRept',chartParams);
        }
        else if(title=='车速转矩图表'){
            $dataChart.dialog('exec', 'spdAndTorqueRept',chartParams);
        }
        else if(title=='电压电流图表'){
            $dataChart.dialog('exec', 'vtgAndCrtRept',chartParams);
        }
        $dataChart.dialog('setOption','title',title);
        $dataChart.dialog('open');
    }
    
})
